package com.xyz.apis.repository;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VerifyCodeRepository {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private JedisService jedisService;

    //过期时间,单位：s
    private int liveTime = 300;

    //hash缓存验证码
    public void setVerifyCode(String phone,String verifyCode){
        try {
            jedisService.set(phone, String.valueOf(verifyCode), liveTime);
        } catch (Exception e) {
            logger.error("redis缓存验证码异常",e);
        }
    }

    //获取验证码
    public String getVerifyCode(String phone){
        try {
            String verifyRedis = jedisService.get(phone);
            return verifyRedis;
        } catch (Exception e) {
            logger.error("redis获取验证码异常,e");
        }
        return null;
    }

    //删除验证码
    public void deleteVerifyCode(String phone){
        try {
            jedisService.del(phone);
        } catch (Exception e) {
            logger.error("redis删除验证码异常,phone:" + phone);
        }
    }
}
